package net.jalbum.jprint;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.print.PageFormat;
import javax.swing.BorderFactory;
import javax.swing.JComponent;

/**
 * Component indicating the current page layout
 *
 * @author david
 */
public class JPageLayout extends JComponent {

    private PageFormat pageFormat;
    private PageLayout pageLayout;
    private int gap;

    public JPageLayout(PageFormat pageFormat, PageLayout pageLayout, int gap) {
        this.pageFormat = pageFormat;
        this.pageLayout = pageLayout;
        this.gap = gap;

        init();
    }

    public PageFormat getPageFormat() {
        return pageFormat;
    }

    public void setPageFormat(PageFormat pageFormat) {
        this.pageFormat = pageFormat;
        repaint();
    }

    public PageLayout getPageLayout() {
        return pageLayout;
    }

    public void setPageLayout(PageLayout pageLayout) {
        this.pageLayout = pageLayout;
        repaint();
    }

    public int getGap() {
        return gap;
    }

    public void setGap(int gap) {
        this.gap = gap;
        repaint();
    }
    
    private boolean isPortrait() {
        return pageFormat.getOrientation() == PageFormat.PORTRAIT;
    }

    @Override
    protected void paintComponent(Graphics g2d) {
        Insets in = getInsets();
        int width = getWidth() - in.left - in.right;
        int height = getHeight() - in.top - in.bottom;

        Graphics2D g = (Graphics2D) g2d;
        if (isOpaque()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        // Ensure insets are respected
        g.translate(in.left, in.top);
        // Ensure fits component
        double scale = Math.min(width / pageFormat.getWidth(), height / pageFormat.getHeight());
        g.scale(scale, scale);
        // Ensure centered
        double largestSide = Math.max(pageFormat.getWidth(), pageFormat.getHeight());
        g.translate((largestSide - pageFormat.getWidth()) / 2, (largestSide - pageFormat.getHeight()) / 2);

        // Paint page
        g.setColor(Color.white);
        g.fillRect(0, 0, (int) pageFormat.getWidth(), (int) pageFormat.getHeight());
        g.setColor(Color.black);
        g.drawRect(0, 0, (int) pageFormat.getWidth(), (int) pageFormat.getHeight());

        // Paint layout tiles
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        
        int cols = isPortrait() ? pageLayout.getRows() : pageLayout.getCols();
        int rows = isPortrait() ? pageLayout.getCols() : pageLayout.getRows();
        g.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        Dimension printable = new Dimension((int) (pageFormat.getImageableWidth()), (int) pageFormat.getImageableHeight());
        int iconicGap = gap*2;
        Dimension tile = new Dimension((printable.width - iconicGap * (cols - 1)) / cols, (printable.height - iconicGap * (rows - 1)) / rows);
        
        g.setColor(Color.lightGray);
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                g.fillRect(col * printable.width / cols, row * printable.height / rows, tile.width, tile.height);
            }
        }
    }

    private void init() {
//        setOpaque(true);
//        setBackground(Color.red);
        Dimension size = new Dimension(96, 96);
        setPreferredSize(size);
        setMaximumSize(size);
        setMinimumSize(size);
        setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    }
}
